package stc.skymobi.demo.test;

import com.skymobi.netty.demo.bean.GetUrlRequest;
import com.skymobi.netty.demo.bean.GetUrlResponse;

import xj.toolkit.app.commons.test.RequestCallback;
import xj.toolkit.base.Sender;
import xj.toolkit.util.Identifier;
import xj.toolkit.util.Identifyable;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration({ "classpath*:testTcpClient.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class TcpClient {

    @Test
    public void json() throws Exception {
    	
    	Thread.sleep(5 * 1000);
    	
        GetUrlRequest request = new GetUrlRequest();
        GetUrlResponse resp = getResponse(request);
        System.out.println(resp);
    }
    
    @Autowired
    @Qualifier("tcpConnector")
	private Sender sender = null;
    
	@Autowired
	private RequestCallback requestCallback;

	protected <T extends Identifyable> T getResponse(Identifyable req) {
		sender.send(req);
		Identifier<?> uuid = req.getIdentification();
		return requestCallback.getResponse(uuid);
	}
}
